<template>
  <div class="login_container">
      <div class="login_box">
          <div class="avatat_box">
              <img src="../assets/logo.png" alt="">
          </div>
          <!-- 登录表单区域 -->
          <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
              <el-form-item prop="username">
                  <el-input placeholder="请输入账户名" v-model=loginForm.username >
                      <i slot="prefix" class="el-icon-user"></i>
                  </el-input>
              </el-form-item>
              <el-form-item prop="password">
                  <el-input placeholder="请输入密码" show-password v-model="loginForm.password">
                      <i slot="prefix" class="el-icon-lock"></i>
                  </el-input>
              </el-form-item>
              <el-form-item class="btns">
                  <el-button type="primary" @click="submit">登录</el-button>
                  <el-button type="info" @click="resetLoginForm">重置</el-button>
              </el-form-item>
          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data(){
      return{
          loginForm:{
              username:'admin',
              password:'123456'
          },
          loginRules:{
               username: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ],
          }         
        }
    },
    methods:{
        // 表单预验证
        submit(){
            this.$refs.loginFormRef.validate(async valid =>{
                if(!valid) return;
                // axios返回的结果是Promise对象，所以给给结果添加 await，validate回调函数添加 async
                // 在用ES6的解构对象{data：res} 把接口返回的数据赋值给res
                const {data:res} = await this.$http.post('login',this.loginForm)
                // console.log(res);
                if(res.meta.status != '200') return this.$message.error(res.meta.msg)
                 this.$message({
                     message:'登录成功！',
                     type:'success',
                     duration:1000,
                     onClose:()=>{
                       this.$router.push('/home')     
                     }
                 })            
                window.sessionStorage.setItem('token',res.data.token)
                
            })
        },
        resetLoginForm(){
            this.$refs.loginFormRef.resetFields();
        }
    }
}
</script>

<style scoped lang='scss'>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: relative;
}
.avatat_box{
    width: 130px;
    height: 130px;
    border-radius: 50%;
    overflow: hidden;
    border: 1px solid #eee;
    position: absolute;
    left: 50%;
    margin-left: -65px;
    top: -65px;
    padding: 10px;
    box-sizing: border-box;
    background-color: #fff;
    img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
    }
}
.el-form{
    width: 300px;
    margin: 100px auto;
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.el-button{   
    width: 80px;
    &--primary{
         background-color: #41b883;
         border-color: #41b883;  
        &:hover{
            background-color: #34976b;
            border-color: #34976b;
        }      
    }
    
}

</style>
